package com.ysh.springbookdemo.controller;

import com.ysh.springbookdemo.model.UserInfo;
import com.ysh.springbookdemo.service.UserInfoService;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController

public class UserController {
    private final UserInfoService userInfoService;

    public UserController(UserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

    @RequestMapping("/login")
    public Boolean login(String name, String password, HttpSession session){
        //1. 参数校验
        //2. 校验账号密码是否正确
        //3. 设置session
        //4. 返回结果
        if(!StringUtils.hasLength(name) || !StringUtils.hasLength(password)){
            return false;
        }
        //根据用户名，获取用户信息
        UserInfo userInfo = userInfoService.queryUserInfoByName(name);
        if(password.equals(userInfo.getPassword())){
            userInfo.setPassword("");
            session.setAttribute("session_user_info",userInfo);
            return true;
        }
        return false;
    }
}
